package com.dianping.cat.consumer.transaction.model.transform;

import com.dianping.cat.consumer.transaction.model.IEntity;
import com.dianping.cat.consumer.transaction.model.IVisitor;
import com.dianping.cat.consumer.transaction.model.entity.AllDuration;
import com.dianping.cat.consumer.transaction.model.entity.Duration;
import com.dianping.cat.consumer.transaction.model.entity.Graph;
import com.dianping.cat.consumer.transaction.model.entity.Graph2;
import com.dianping.cat.consumer.transaction.model.entity.GraphTrend;
import com.dianping.cat.consumer.transaction.model.entity.Machine;
import com.dianping.cat.consumer.transaction.model.entity.Range;
import com.dianping.cat.consumer.transaction.model.entity.Range2;
import com.dianping.cat.consumer.transaction.model.entity.StatusCode;
import com.dianping.cat.consumer.transaction.model.entity.TransactionName;
import com.dianping.cat.consumer.transaction.model.entity.TransactionReport;
import com.dianping.cat.consumer.transaction.model.entity.TransactionType;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;












































public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;
  
/*  75 */   public DefaultXmlBuilder() { this(false); }



/*  79 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }
  
  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  83 */     this.m_compact = compact;
/*  84 */     this.m_sb = sb;
/*  85 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }
  
  public String buildXml(IEntity<?> entity) {
/*  89 */     entity.accept(this.m_visitor);
/*  90 */     return this.m_sb.toString();
  }
  
  protected void endTag(String name) {
/*  94 */     this.m_level--;

/*  96 */     indent();
/*  97 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  
/* 101 */   protected String escape(Object value) { return escape(value, false); }

  
  protected String escape(Object value, boolean text) {
/* 105 */     if (value == null) {
/* 106 */       return null;
    }
    
/* 109 */     String str = toString(value);
/* 110 */     int len = str.length();
/* 111 */     StringBuilder sb = new StringBuilder(len + 16);

/* 113 */     for (int i = 0; i < len; i++) {
/* 114 */       char ch = str.charAt(i);

/* 116 */       switch (ch) {
        case '<':
/* 118 */           sb.append("&lt;");
          break;
        case '>':
/* 121 */           sb.append("&gt;");
          break;
        case '&':
/* 124 */           sb.append("&amp;");
          break;
        case '"':
/* 127 */           if (!text) {
/* 128 */             sb.append("&quot;");
            break;
          }
        default:
/* 132 */           sb.append(ch);
          break;
      }
    
    }
/* 137 */     return sb.toString();
  }
  
  protected void indent() {
/* 141 */     if (!this.m_compact) {
/* 142 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 143 */         this.m_sb.append("   ");
      }
    }
  }

  
/* 149 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 153 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 157 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }

  
  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 161 */     indent();

/* 163 */     this.m_sb.append('<').append(name);

/* 165 */     int len = nameValues.length;

/* 167 */     for (int i = 0; i + 1 < len; i += 2) {
/* 168 */       Object attrName = nameValues[i];
/* 169 */       Object attrValue = nameValues[i + 1];

/* 171 */       if (attrValue != null) {
/* 172 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }
    
/* 176 */     if (dynamicAttributes != null) {
/* 177 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 178 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }
    
/* 182 */     if (text != null && closed) {
/* 183 */       this.m_sb.append('>');
/* 184 */       this.m_sb.append(escape(text, true));
/* 185 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 187 */       if (closed) {
/* 188 */         this.m_sb.append('/');
      } else {
/* 190 */         this.m_level++;
      }
      
/* 193 */       this.m_sb.append(">\r\n");
    }
  }

  
  protected String toString(Object value) {
/* 199 */     if (value instanceof String)
/* 200 */       return (String)value; 
/* 201 */     if (value instanceof Collection) {
/* 202 */       Collection<Object> list = (Collection)value;
/* 203 */       StringBuilder sb = new StringBuilder(32);
/* 204 */       boolean first = true;

/* 206 */       for (Object item : list) {
/* 207 */         if (first) {
/* 208 */           first = false;
        } else {
/* 210 */           sb.append(',');
        }
        
/* 213 */         if (item != null) {
/* 214 */           sb.append(item);
        }
      }
      
/* 218 */       return sb.toString();
/* 219 */     }  if (value.getClass().isArray()) {
/* 220 */       int len = Array.getLength(value);
/* 221 */       StringBuilder sb = new StringBuilder(32);
/* 222 */       boolean first = true;

/* 224 */       for (int i = 0; i < len; i++) {
/* 225 */         Object item = Array.get(value, i);

/* 227 */         if (first) {
/* 228 */           first = false;
        } else {
/* 230 */           sb.append(',');
        }
        
/* 233 */         if (item != null) {
/* 234 */           sb.append(item);
        }
      }
      
/* 238 */       return sb.toString();
    }
    
/* 241 */     return String.valueOf(value);
  }
  
  protected void tagWithText(String name, String text, Object... nameValues) {
/* 245 */     if (text == null) {
      return;
    }
    
/* 249 */     indent();

/* 251 */     this.m_sb.append('<').append(name);

/* 253 */     int len = nameValues.length;

/* 255 */     for (int i = 0; i + 1 < len; i += 2) {
/* 256 */       Object attrName = nameValues[i];
/* 257 */       Object attrValue = nameValues[i + 1];

/* 259 */       if (attrValue != null) {
/* 260 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }
    
/* 264 */     this.m_sb.append(">");
/* 265 */     this.m_sb.append(escape(text, true));
/* 266 */     this.m_sb.append("</").append(name).append(">\r\n");
  }
  
  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 270 */     if (text == null || text.equals(defaultValue)) {
      return;
    }
    
/* 274 */     indent();

/* 276 */     this.m_sb.append('<').append(name).append(">");

/* 278 */     if (escape) {
/* 279 */       this.m_sb.append(escape(text, true));
    } else {
/* 281 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }
    
/* 284 */     this.m_sb.append("</").append(name).append(">\r\n");
  }
  
  protected String toString(Date date, String format) {
/* 288 */     if (date != null) {
/* 289 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 291 */     return null;
  }

  
  protected String toString(Number number, String format) {
/* 296 */     if (number != null) {
/* 297 */       return (new DecimalFormat(format)).format(number);
    }
/* 299 */     return null;
  }



  
/* 305 */   public void visitAllDuration(AllDuration allDuration) { startTag("all-duration", true, null, new Object[] { "value", Integer.valueOf(allDuration.getValue()), "count", Integer.valueOf(allDuration.getCount()) }); }




/* 310 */   public void visitDuration(Duration duration) { startTag("duration", true, null, new Object[] { "value", Integer.valueOf(duration.getValue()), "count", Integer.valueOf(duration.getCount()) }); }




/* 315 */   public void visitGraph(Graph graph) { startTag("graph", true, null, new Object[] { "duration", Integer.valueOf(graph.getDuration()), "sum", graph.getSum(), "avg", graph.getAvg(), "count", graph.getCount(), "fails", graph.getFails() }); }




/* 320 */   public void visitGraph2(Graph2 graph2) { startTag("graph2", true, null, new Object[] { "duration", Integer.valueOf(graph2.getDuration()), "sum", graph2.getSum(), "avg", graph2.getAvg(), "count", graph2.getCount(), "fails", graph2.getFails() }); }




/* 325 */   public void visitGraphTrend(GraphTrend graphTrend) { startTag("graph-trend", true, null, new Object[] { "duration", Integer.valueOf(graphTrend.getDuration()), "sum", graphTrend.getSum(), "avg", graphTrend.getAvg(), "count", graphTrend.getCount(), "fails", graphTrend.getFails() }); }


  
  public void visitMachine(Machine machine) {
/* 330 */     startTag("machine", null, new Object[] { "ip", machine.getIp() });

/* 332 */     if (!machine.getTypes().isEmpty()) {
/* 333 */       for (TransactionType type : machine.getTypes().values()) {
/* 334 */         type.accept(this.m_visitor);
      }
    }
    
/* 338 */     endTag("machine");
  }

  
  public void visitName(TransactionName name) {
/* 343 */     startTag("name", null, new Object[] { "id", name.getId(), "totalCount", Long.valueOf(name.getTotalCount()), "failCount", Long.valueOf(name.getFailCount()), "failPercent", toString(Double.valueOf(name.getFailPercent()), "0.00"), "min", toString(Double.valueOf(name.getMin()), "0.00"), "max", toString(Double.valueOf(name.getMax()), "0.00"), "avg", toString(Double.valueOf(name.getAvg()), "0.0"), "sum", toString(Double.valueOf(name.getSum()), "0.0"), "sum2", toString(Double.valueOf(name.getSum2()), "0.0"), "std", toString(Double.valueOf(name.getStd()), "0.0"), "tps", toString(Double.valueOf(name.getTps()), "0.00"), "line95Value", toString(Double.valueOf(name.getLine95Value()), "0.00"), "line99Value", toString(Double.valueOf(name.getLine99Value()), "0.00"), "line999Value", toString(Double.valueOf(name.getLine999Value()), "0.00"), "line90Value", toString(Double.valueOf(name.getLine90Value()), "0.00"), "line50Value", toString(Double.valueOf(name.getLine50Value()), "0.00"), "line9999Value", toString(Double.valueOf(name.getLine9999Value()), "0.00") });

/* 345 */     element("successMessageUrl", name.getSuccessMessageUrl(), null, true);

/* 347 */     element("failMessageUrl", name.getFailMessageUrl(), null, true);

/* 349 */     element("longestMessageUrl", name.getLongestMessageUrl(), null, true);

/* 351 */     if (!name.getRanges().isEmpty()) {
/* 352 */       for (Range range : name.getRanges().values()) {
/* 353 */         range.accept(this.m_visitor);
      }
    }
    
/* 357 */     if (!name.getDurations().isEmpty()) {
/* 358 */       for (Duration duration : name.getDurations().values()) {
/* 359 */         duration.accept(this.m_visitor);
      }
    }
    
/* 363 */     if (!name.getGraphs().isEmpty()) {
/* 364 */       for (Graph graph : name.getGraphs().values()) {
/* 365 */         graph.accept(this.m_visitor);
      }
    }
    
/* 369 */     if (name.getGraphTrend() != null) {
/* 370 */       name.getGraphTrend().accept(this.m_visitor);
    }
    
/* 373 */     if (!name.getStatusCodes().isEmpty()) {
/* 374 */       for (StatusCode statusCode : name.getStatusCodes().values()) {
/* 375 */         statusCode.accept(this.m_visitor);
      }
    }
    
/* 379 */     endTag("name");
  }


  
/* 384 */   public void visitRange(Range range) { startTag("range", true, null, new Object[] { "value", Integer.valueOf(range.getValue()), "count", Integer.valueOf(range.getCount()), "sum", Double.valueOf(range.getSum()), "avg", toString(Double.valueOf(range.getAvg()), "0.0"), "fails", Integer.valueOf(range.getFails()), "min", toString(Double.valueOf(range.getMin()), "0.00"), "max", toString(Double.valueOf(range.getMax()), "0.00"), "line95Value", toString(Double.valueOf(range.getLine95Value()), "0.00"), "line99Value", toString(Double.valueOf(range.getLine99Value()), "0.00"), "line999Value", toString(Double.valueOf(range.getLine999Value()), "0.00"), "line90Value", toString(Double.valueOf(range.getLine90Value()), "0.00"), "line50Value", toString(Double.valueOf(range.getLine50Value()), "0.00"), "line9999Value", toString(Double.valueOf(range.getLine9999Value()), "0.00") }); }




/* 389 */   public void visitRange2(Range2 range2) { startTag("range2", true, null, new Object[] { "value", Integer.valueOf(range2.getValue()), "count", Integer.valueOf(range2.getCount()), "sum", Double.valueOf(range2.getSum()), "avg", toString(Double.valueOf(range2.getAvg()), "0.0"), "fails", Integer.valueOf(range2.getFails()), "min", toString(Double.valueOf(range2.getMin()), "0.00"), "max", toString(Double.valueOf(range2.getMax()), "0.00"), "line95Value", toString(Double.valueOf(range2.getLine95Value()), "0.00"), "line99Value", toString(Double.valueOf(range2.getLine99Value()), "0.00"), "line999Value", toString(Double.valueOf(range2.getLine999Value()), "0.00"), "line90Value", toString(Double.valueOf(range2.getLine90Value()), "0.00"), "line50Value", toString(Double.valueOf(range2.getLine50Value()), "0.00"), "line9999Value", toString(Double.valueOf(range2.getLine9999Value()), "0.00") }); }




/* 394 */   public void visitStatusCode(StatusCode statusCode) { startTag("statusCode", true, null, new Object[] { "id", statusCode.getId(), "count", Long.valueOf(statusCode.getCount()) }); }


  
  public void visitTransactionReport(TransactionReport transactionReport) {
/* 399 */     startTag("transaction-report", null, new Object[] { "domain", transactionReport.getDomain(), "startTime", toString(transactionReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(transactionReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 401 */     if (!transactionReport.getDomainNames().isEmpty()) {
/* 402 */       for (String domain : transactionReport.getDomainNames()) {
/* 403 */         tagWithText("domain", domain, new Object[0]);
      }
    }
    
/* 407 */     if (!transactionReport.getIps().isEmpty()) {
/* 408 */       for (String ip : transactionReport.getIps()) {
/* 409 */         tagWithText("ip", ip, new Object[0]);
      }
    }
    
/* 413 */     if (!transactionReport.getMachines().isEmpty()) {
/* 414 */       for (Machine machine : transactionReport.getMachines().values()) {
/* 415 */         machine.accept(this.m_visitor);
      }
    }
    
/* 419 */     endTag("transaction-report");
  }

  
  public void visitType(TransactionType type) {
/* 424 */     startTag("type", type.getDynamicAttributes(), new Object[] { "id", type.getId(), "totalCount", Long.valueOf(type.getTotalCount()), "failCount", Long.valueOf(type.getFailCount()), "failPercent", toString(Double.valueOf(type.getFailPercent()), "0.00"), "min", toString(Double.valueOf(type.getMin()), "0.00"), "max", toString(Double.valueOf(type.getMax()), "0.00"), "avg", toString(Double.valueOf(type.getAvg()), "0.0"), "sum", toString(Double.valueOf(type.getSum()), "0.0"), "sum2", toString(Double.valueOf(type.getSum2()), "0.0"), "std", toString(Double.valueOf(type.getStd()), "0.0"), "tps", toString(Double.valueOf(type.getTps()), "0.00"), "line95Value", toString(Double.valueOf(type.getLine95Value()), "0.00"), "line99Value", toString(Double.valueOf(type.getLine99Value()), "0.00"), "line999Value", toString(Double.valueOf(type.getLine999Value()), "0.00"), "line90Value", toString(Double.valueOf(type.getLine90Value()), "0.00"), "line50Value", toString(Double.valueOf(type.getLine50Value()), "0.00"), "line9999Value", toString(Double.valueOf(type.getLine9999Value()), "0.00") });

/* 426 */     element("successMessageUrl", type.getSuccessMessageUrl(), null, true);

/* 428 */     element("failMessageUrl", type.getFailMessageUrl(), null, true);

/* 430 */     element("longestMessageUrl", type.getLongestMessageUrl(), null, true);

/* 432 */     if (!type.getNames().isEmpty()) {
/* 433 */       for (TransactionName name : type.getNames().values()) {
/* 434 */         name.accept(this.m_visitor);
      }
    }
    
/* 438 */     if (!type.getGraph2s().isEmpty()) {
/* 439 */       for (Graph2 graph2 : type.getGraph2s().values()) {
/* 440 */         graph2.accept(this.m_visitor);
      }
    }
    
/* 444 */     if (type.getGraphTrend() != null) {
/* 445 */       type.getGraphTrend().accept(this.m_visitor);
    }
    
/* 448 */     if (!type.getRange2s().isEmpty()) {
/* 449 */       for (Range2 range2 : type.getRange2s().values()) {
/* 450 */         range2.accept(this.m_visitor);
      }
    }
    
/* 454 */     endTag("type");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/transaction/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */